/* eslint-disable react/jsx-key */
import React, { useState, useRef } from 'react';
import ReactDOM from 'react-dom';
import { Image, Switch, Button, Modal, message, Popconfirm } from 'antd';
import AutoTable from '@/components/AutoTable';
import InitForm from '@/components/InitForm';
import { deletepayment, payment, store } from '@/services/basic';
import { connect } from 'umi';
import { PlusOutlined } from '@ant-design/icons';
import { doFetch } from '@/utils/doFetch';

// type 类型有 table treeselect upload inputnumber datepicker daterange radio select textarea autoinput editor password input

let defaultFields = {
  name: {
    value: null,
    type: 'input',
    title: '投诉类型',
    name: ['name'],
    required: true,
    col: {
      span: 24,
    },
  },
};

function Things(props) {
  const [vs, cvs] = useState(false), //表单显/隐
    [fields, cf] = useState(defaultFields),
    [iftype, ciftype] = useState({});

  let actionRef = useRef(),
    ChildRef = null;

  function reload() {
    actionRef?.current?.reload();
    ChildRef?.onRefresh();
  }

  const columns = [
    {
      title: '投诉类型',
      dataIndex: 'name',
      key: 'name',
      search: false,
    },
    {
      title: '操作',
      valueType: 'option',
      width: 100,
      render: (text, record, _, action) => [
        <a
          style={{ color: '#1890ff' }}
          onClick={() => {
            cvs(true);
            cf((fields) => {
              for (let i in fields) {
                fields[i].value = record[i];
              }
              return { ...fields };
            });
            ciftype({
              val: 'edit',
              title: '编辑投诉类型',
              id: record.id,
            });
          }}
        >
          编辑
        </a>,
        <Popconfirm
          placement="bottom"
          title={'确认删除该投诉类型？'}
          onConfirm={() => {
            doFetch({
              url: '/wbn-api/complaint_setting/' + record?.id,
              params: {},
              method: 'DELETE',
            }).then((res) => {
              if (res.code == 0) {
                message.success('操作成功');
                reload();
              }
            });
          }}
          okText="删除"
          cancelText="取消"
        >
          <a style={{ color: '#f50' }}>删除</a>
        </Popconfirm>,
      ],
    },
  ];

  let extrarender = [
    <div
      className="action"
      title="新增"
      onClick={() => {
        cvs(true);
        cf((fields) => {
          for (let i in fields) {
            fields[i].value = null;
          }
          return { ...fields };
        });
        ciftype({
          val: 'add',
          title: '新增投诉类型',
        });
      }}
    >
      <PlusOutlined />
    </div>,
  ];

  let saveData = (values) => {
    if (iftype.val == 'add') {
      doFetch({
        url: '/wbn-api/complaint_setting',
        params: values,
      }).then((res) => {
        if (res.code == 0) {
          message.success('操作成功');
          reload();
          cvs(false);
        }
      });
    } else if (iftype.val == 'edit') {
      doFetch({
        url: '/wbn-api/complaint_setting/' + iftype.id,
        params: values,
        method: 'PUT',
      }).then((res) => {
        if (res.code == 0) {
          message.success('操作成功');
          reload();
          cvs(false);
        }
      });
    }
  };

  return (
    <div className="pgcontainer">
      <AutoTable
        pagetitle={props.route.name} //页面标题
        pageextra={extrarender} //页面操作 新增or批量删除
        columns={columns}
        actionRef={actionRef}
        onRef={(node) => (ChildRef = node)}
        path="/wbn-api/complaint_setting"
        //withCard={false}
        //cardtitle = "name" //卡片标题
        //cardavatar = "img_url" //卡片头像
      />
      <Modal
        maskClosable={false}
        title={iftype.title}
        open={vs}
        onCancel={() => cvs(false)}
        footer={false}
        width={600}
        destroyOnClose={true}
      >
        <InitForm
          fields={fields}
          submitData={(values) => {
            saveData(values);
          }}
          onChange={(changedValues, allValues) => {
            //联动操作
          }}
          submitting={props.loading.effects['basic/addpayment'] || !vs}
        />
      </Modal>
    </div>
  );
}

export default connect(({ basic, loading }) => ({
  basic,
  loading,
}))(Things);
